package com.zdsoft.blp.zongbuju;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.net.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pc on 2016/9/5.
 */
public class FirstOpenActivity extends BaseActivity {
    private ViewPager vp;
    private int[] images = new int[]{R.drawable.zxx,R.drawable.llq,R.drawable.tsh,R.drawable.bml};
    private ArrayList<View> pageview;
    private ImageView iv,iv1,iv2,iv3,iv4;
    private List<ImageView> ivs;
    @Override
    protected void init() {
        setContentView(R.layout.firstopenlayout);
        vp = find(R.id.vp);
        iv1 = find(R.id.iv1);
        iv2 = find(R.id.iv2);
        iv3 = find(R.id.iv3);
        iv4 = find(R.id.iv4);
        ivs = new ArrayList<>();
        ivs.add(iv1);
        ivs.add(iv2);
        ivs.add(iv3);
        ivs.add(iv4);
        pageview = new ArrayList<>();
        LayoutInflater inflater =getLayoutInflater();
        for(int i =0;i<images.length; i++){
            View view = inflater.inflate(R.layout.firstitem, null);
            iv= (ImageView) view.findViewById(R.id.iv);
            iv.setImageResource(images[i]);
            pageview.add(view);
        }
        vp.setAdapter(pagerAdapter);
        //设置第一个页面的选中状态
        ivs.get(0).setImageResource(R.drawable.icon_point_pre);
        //vp滑动监听事件
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                clean();
                ivs.get(position).setImageResource(R.drawable.icon_point_pre);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstOpenActivity.this,MainActivity.class);
                startActivity(intent);
                FirstOpenActivity.this.finish();
            }
        });

    }

    private void clean() {
        for(int i =0;i<ivs.size();i++){
            ivs.get(i).setImageResource(R.drawable.icon_point);
        }
    }

    //数据适配器
    PagerAdapter pagerAdapter = new PagerAdapter(){
        @Override
        //获取当前窗体界面数
        public int getCount() {
            return pageview.size();
        }
        @Override
        //断是否由对象生成界面
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0==arg1;
        }
        //是从ViewGroup中移出当前View
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(pageview.get(arg1));
        }
        //返回一个对象，这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中
        public Object instantiateItem(View arg0, int arg1){
            ((ViewPager)arg0).addView(pageview.get(arg1));
            return pageview.get(arg1);
        }
    };


}
